package com.Ajava1704.for设计模式.for动态代理.CGLIB;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 作者: LCG
 * 日期: 2020/2/14 09:32
 * 描述:
 */
public class ForMyMethodInterceptor2<T> implements MethodInterceptor {

    private T t;

    public ForMyMethodInterceptor2(T t) {
        this.t = t;
    }

    private Enhancer enhancer=new Enhancer();

    public T getObjectProxy(Class zclass){

        enhancer.setSuperclass(zclass);
        enhancer.setCallback(this);
        return (T) enhancer.create();

    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("=========原方法执行前的增强==========");
        Object invoke = method.invoke(t, objects);
        System.out.println("=========原方法执行后的增强==========");

        return invoke;
    }
}
